#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'teffy'

'''
@created on 17-11-8
@poject:python-shoot-game
@author:teffy
'''

import pygame
from pygame.sprite import Sprite
from settings import Settings

class Ship(Sprite):
    def __init__(self, screen, settings):
        super(Ship,self).__init__()
        '''初始化飞船并设置初始位置'''
        self.settings = settings
        self.screen = screen
        # 加载图片，获取外接矩形
        self.image = pygame.image.load('imgs/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        # 将新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        self.center = float(self.rect.centerx)
        self.moving_right = False
        self.moving_left = False

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_right and self.center < self.screen_rect.right - (self.rect.right - self.rect.left) / 2:
            self.center += self.settings.ship_speed_factor
        elif self.moving_left and self.center > self.screen_rect.left + (self.rect.right - self.rect.left) / 2:
            self.center -= self.settings.ship_speed_factor

        self.rect.centerx = self.center

    def center_ship(self):
        self.center = self.rect.centerx
